package com.gin.kotlin.no_gay_alliance.fragment.topiclist

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.MenuItem
import android.view.View
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.activity.TopicListActivity
import com.gin.kotlin.no_gay_alliance.activity.TopicListActivity.Companion.KEY_ARGUMENT
import com.gin.kotlin.no_gay_alliance.argument.TopicListArgument
import com.gin.kotlin.no_gay_alliance.databinding.FragmentTopicListColListBinding
import com.gin.kotlin.no_gay_alliance.model.SearchAreaModel
import com.gin.nga.enums.ForumType
import com.gin.nga.enums.param.OrderByParam

/**
 * 浏览合集
 */
class ColListFragment : BaseTopicListFragment<FragmentTopicListColListBinding>(), View.OnClickListener {

    override fun onCreateView() {
        super.onCreateView()
        val b = this.b ?: return
        // 顶部菜单栏
        listOf(
            b.tvTopMenuBlocked,
            b.tvTopMenuTopTopic,
        ).forEach { it.setOnClickListener(this) }

    }



    override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
        return when (menuItem.itemId) {
            R.id.mi_search -> startSearch()
            else -> super.onMenuItemSelected(menuItem)
        }
    }

    private fun startSearch(): Boolean {
        val cf = currentForum ?: return false
        val searchAreas = mutableListOf(SearchAreaModel(ForumType.COL, cf.colTid!!, cf.colTitle!!, cf.getBigIconUrl()!!, true))
        TopicSearchFragment.startActivityCol(requireContext(), searchAreas)
        Log.i(TAG, "打开合集搜索")
        return true
    }


    companion object {
        fun newInstance(argument: TopicListArgument) = ColListFragment()
            .also { it.arguments = Bundle().also { b -> b.putParcelable(KEY_ARGUMENT, argument) } }

        /**
         * 启动活动
         */
        fun startActivity(
            context: Context,
            colTid: Long,
            page: Int? = null,
            orderBy: OrderByParam? = null,
            title: String? = null
        ) = TopicListActivity.startActivity(
            context, TopicListArgument(
                TopicListArgument.Type.COL_LIST,
                colTid = colTid,
                page = page,
                orderBy = orderBy?.value,
                title = title
            )
        )
    }
}